﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace FFWorker
{
    public class RemoveReadOnlyAttributeDialog : CommonDialog
    {
        private RemoveReadOnlyAttributeDialogResult Result;

        private System.Windows.Forms.Form RemoveReadOnlyAttributeFolderForm;
        private System.Windows.Forms.Button DialogYesButton;
        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.Button DialogNoButton;
        private System.Windows.Forms.Button DialogYesToAllButton;
        private System.Windows.Forms.Button DialogNoToAllButton;
        private System.Windows.Forms.Label DialogLabel;

        private void InitializeComponent()
        {
            this.RemoveReadOnlyAttributeFolderForm = new Form();
            this.DialogYesButton = new System.Windows.Forms.Button();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.DialogNoButton = new System.Windows.Forms.Button();
            this.DialogYesToAllButton = new System.Windows.Forms.Button();
            this.DialogNoToAllButton = new System.Windows.Forms.Button();
            this.DialogLabel = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            RemoveReadOnlyAttributeFolderForm.SuspendLayout();
            // 
            // DeleteYesButton
            // 
            this.DialogYesButton.Location = new System.Drawing.Point(12, 144);
            this.DialogYesButton.Name = "DeleteYesButton";
            this.DialogYesButton.Size = new System.Drawing.Size(75, 23);
            this.DialogYesButton.TabIndex = 0;
            this.DialogYesButton.Text = "Yes";
            this.DialogYesButton.UseVisualStyleBackColor = true;
            // 
            // pictureBox1
            // 
            this.pictureBox1.Image = global::FFWorker.Properties.Resources.otaznik;
            this.pictureBox1.Location = new System.Drawing.Point(110, 12);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(100, 79);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox1.TabIndex = 1;
            this.pictureBox1.TabStop = false;
            // 
            // DeleteNoButton
            // 
            this.DialogNoButton.Location = new System.Drawing.Point(255, 144);
            this.DialogNoButton.Name = "DeleteNoButton";
            this.DialogNoButton.Size = new System.Drawing.Size(75, 23);
            this.DialogNoButton.TabIndex = 2;
            this.DialogNoButton.Text = "No";
            this.DialogNoButton.UseVisualStyleBackColor = true;
            // 
            // DeleteYesToAllButton
            // 
            this.DialogYesToAllButton.Location = new System.Drawing.Point(93, 144);
            this.DialogYesToAllButton.Name = "DeleteYesToAllButton";
            this.DialogYesToAllButton.Size = new System.Drawing.Size(75, 23);
            this.DialogYesToAllButton.TabIndex = 3;
            this.DialogYesToAllButton.Text = "Yes to all";
            this.DialogYesToAllButton.UseVisualStyleBackColor = true;
            // 
            // DeleteNoToAllButton
            // 
            this.DialogNoToAllButton.Location = new System.Drawing.Point(174, 144);
            this.DialogNoToAllButton.Name = "DeleteNoToAllButton";
            this.DialogNoToAllButton.Size = new System.Drawing.Size(75, 23);
            this.DialogNoToAllButton.TabIndex = 4;
            this.DialogNoToAllButton.Text = "No to all";
            this.DialogNoToAllButton.UseVisualStyleBackColor = true;
            // 
            // DeleteLabel
            // 
            this.DialogLabel.AutoSize = true;
            this.DialogLabel.Location = new System.Drawing.Point(27, 107);
            this.DialogLabel.Name = "DeleteLabel";
            this.DialogLabel.Size = new System.Drawing.Size(0, 13);
            this.DialogLabel.TabIndex = 5;
            // 
            // DeleteFolderForm
            // 
            RemoveReadOnlyAttributeFolderForm.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            RemoveReadOnlyAttributeFolderForm.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            RemoveReadOnlyAttributeFolderForm.BackColor = System.Drawing.SystemColors.ControlLightLight;
            RemoveReadOnlyAttributeFolderForm.ClientSize = new System.Drawing.Size(343, 179);
            RemoveReadOnlyAttributeFolderForm.Controls.Add(this.DialogLabel);
            RemoveReadOnlyAttributeFolderForm.Controls.Add(this.DialogNoToAllButton);
            RemoveReadOnlyAttributeFolderForm.Controls.Add(this.DialogYesToAllButton);
            RemoveReadOnlyAttributeFolderForm.Controls.Add(this.DialogNoButton);
            RemoveReadOnlyAttributeFolderForm.Controls.Add(this.pictureBox1);
            RemoveReadOnlyAttributeFolderForm.Controls.Add(this.DialogYesButton);
            RemoveReadOnlyAttributeFolderForm.Name = "DeleteFolderForm";
            RemoveReadOnlyAttributeFolderForm.Text = "Delete Folder";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            RemoveReadOnlyAttributeFolderForm.ResumeLayout(false);
            RemoveReadOnlyAttributeFolderForm.PerformLayout();

            this.DialogYesButton.Click += new EventHandler(DeleteYesButton_Click);
            this.DialogYesToAllButton.Click += new EventHandler(DeleteYesToAllButton_Click);
            this.DialogNoButton.Click += new EventHandler(DeleteNoButton_Click);
            this.DialogNoToAllButton.Click += new EventHandler(DeleteNoToAllButton_Click);
        }

        protected override bool RunDialog(System.IntPtr hwndOwner)
        {
            InitializeComponent();

            if (RemoveReadOnlyAttributeFolderForm.ShowDialog() == DialogResult.OK)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        public override void Reset()
        {

        }

        private void DeleteYesButton_Click(object sender, EventArgs e)
        {
            this.Result = RemoveReadOnlyAttributeDialogResult.YES;

            RemoveReadOnlyAttributeFolderForm.DialogResult = DialogResult.OK;

            RemoveReadOnlyAttributeFolderForm.Close();
        }

        private void DeleteYesToAllButton_Click(object sender, EventArgs e)
        {
            this.Result = RemoveReadOnlyAttributeDialogResult.YES_TO_ALL;

            RemoveReadOnlyAttributeFolderForm.DialogResult = DialogResult.OK;

            RemoveReadOnlyAttributeFolderForm.Close();
        }

        private void DeleteNoButton_Click(object sender, EventArgs e)
        {
            this.Result = RemoveReadOnlyAttributeDialogResult.NO;

            RemoveReadOnlyAttributeFolderForm.DialogResult = DialogResult.OK;

            RemoveReadOnlyAttributeFolderForm.Close();
        }

        private void DeleteNoToAllButton_Click(object sender, EventArgs e)
        {
            this.Result = RemoveReadOnlyAttributeDialogResult.NO_TO_ALL;

            RemoveReadOnlyAttributeFolderForm.DialogResult = DialogResult.OK;

            RemoveReadOnlyAttributeFolderForm.Close();
        }

        public RemoveReadOnlyAttributeDialogResult ShowFormDialog()
        {
            this.ShowDialog();

            return this.Result;
        }
    }

    public class Folder
    {
        private DirectoryInfo FolderInfo;

        public Folder(string Path)
        {
            FolderInfo = new DirectoryInfo(Path);
        }

        static public void RemoveReadOnlyAttribute(String Directory)
        {
            DirectoryInfo Dir = new DirectoryInfo(Directory);

            foreach (var File in Dir.GetFiles("*", SearchOption.AllDirectories))
            {
                if ((File.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                {
                    File.Attributes &= ~(FileAttributes.ReadOnly);
                }
            }   
        }

        static public bool AnalyseReadOnlyAttribute(String Directory)
        {
            DirectoryInfo Dir = new DirectoryInfo(Directory);

            foreach (var File in Dir.GetFiles("*", SearchOption.AllDirectories))
            {
                if ((File.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                {
                    return true;
                }
            }           

            return false;
        }

        static public void DeleteFolder(String Dir, bool DeleteSubDirToo)
        {
            try
            {
                if(Directory.Exists(Dir))
                    Directory.Delete(Dir, true);
            }
            catch (Exception ex)
            {
                MessageBox.Show("While deleting the folder the " + ex + " occured", "Exception",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

    public enum RemoveReadOnlyAttributeDialogResult { YES = 1, YES_TO_ALL, NO, NO_TO_ALL };
}
